<script setup>
import {showToast} from 'vant';
import {ref} from "vue";

const searchResult = ref([]);
import {areaList} from '@vant/area-data';
import {useRouter} from "vue-router";
import {addAddress, chanceAddress} from "@/api/address.js";
import {getUserLocal} from "@/utils/auth.js";

const props = defineProps(['addressInfo', 'addressId']);
// const route = useRoute();
const router = useRouter();
// const addressInfo = ref(JSON.parse(route.query.addressInfo))
const back = () => {
  router.push({
    name: 'address',
  });
};
console.log(props.addressId, props.addressInfo);
const onSave = (AddressEditInfo) => {
  showToast('save')
  // const up = ref({
  //   addressId: props.addressId,
  //   receiver: AddressEditInfo.name,
  //   province: AddressEditInfo.province,
  //   city: AddressEditInfo.city,
  //   county: '中国',
  //   address: AddressEditInfo.county + ' ' + AddressEditInfo.addressDetail,
  //   areaCode: AddressEditInfo.areaCode,
  //   cellPhone: AddressEditInfo.tel,
  //   fixedPhone: '87059870',
  //   postCode: AddressEditInfo.areaCode,
  //   tag: '',
  //   isDefault: AddressEditInfo.isDefault ? 1 : 0
  // })
  // console.log(up)
  if (props.addressId === '-1') {
    const add = ref({
      uid: getUserLocal().uid,
      receiver: AddressEditInfo.name,
      province: AddressEditInfo.province,
      city: AddressEditInfo.city,
      county: '中国',
      address: AddressEditInfo.county + AddressEditInfo.addressDetail,
      areaCode: AddressEditInfo.areaCode,
      cellPhone: AddressEditInfo.tel,
      fixedPhone: '87059870',
      postCode: AddressEditInfo.areaCode,
      tag: '',
      isDefault: AddressEditInfo.isDefault ? 1 : 0
    })
    addAddress(add.value).then(result => {
      console.log(result)
      if (result.data.code === 0) {
        showToast('添加成功')
        router.push("/address")
      }
    })
  } else {
    const up = ref({
      addressId: props.addressId,
      receiver: AddressEditInfo.name,
      province: AddressEditInfo.province,
      city: AddressEditInfo.city,
      county: '中国',
      address: AddressEditInfo.county + AddressEditInfo.addressDetail,
      areaCode: AddressEditInfo.areaCode,
      cellPhone: AddressEditInfo.tel,
      fixedPhone: '87059870',
      postCode: AddressEditInfo.areaCode,
      tag: '',
      isDefault: AddressEditInfo.isDefault ? 1 : 0
    })
    chanceAddress(up.value).then(result => {
      console.log(result)
      if (result.data.code === 0) {
        showToast('修改成功')
        router.push("/address")

      }
    })
  }
};
const onDelete = () => showToast('delete');
// {name: '张三', tel: '13000000000', province: '浙江省',city:'杭州市',county:'西湖区', addressDetail: '文三路 138 号东方通信大厦 7 楼 501 室', isDefault: true}
// const onChangeDetail = (val) => {
//   if (val) {
//     searchResult.value = [
//       {
//         name: '黄龙万科中心',
//         address: '杭州市西湖区',
//       },
//     ];
//   } else {
//     searchResult.value = [];
//   }
// };
</script>

<template>
  <div>
    <van-nav-bar
        title="编辑地址"
        left-text="返回"
        left-arrow
        @click-left="back">
    </van-nav-bar>
    <van-address-edit
        :area-list="areaList"
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        :address-info="JSON.parse(props.addressInfo)"
    />
  </div>
</template>

<style scoped>

</style>